import { Injectable } from '@angular/core';
import { App, Platform } from 'ionic-angular';
import { Buffer } from 'buffer';
/*
  Generated class for the UnitsProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class UnitsProvider {

	constructor(
		public app: App,
		public platform: Platform
	) {
		console.log('Hello UnitsProvider Provider');
	}
	//微信内置浏览器
	public isWechat() {
		let ua = window.navigator.userAgent.toLowerCase();
		let item = new RegExp('micromessenger', 'i');
		if (item.test(ua)) {
			return true;
		} else {
			return false;
		}
	}
	//是否app
	public isApp() {
		if (this.platform.is('cordova')) {
			return true;
		} else {
			return false;
		}
	}
	//获取get参数
	public urlParams(name) {
		let lets = new Object();
		let hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for (let i = 0; i < hashes.length; i++) {
			let done = hashes[i].split('#');
			if (done.length > 1) {
				let hash = done[0].split('=');
				lets[hash[0]] = hash[1];
			} else {
				let hash = hashes[i].split('=');
				lets[hash[0]] = hash[1];
			}
		}
		return lets[name];
	};

	//时间戳转换
	public getIntToDateString(timestamp) {
		let theDate = new Date();
		theDate.setTime(timestamp);
		return theDate.getFullYear() + "-" + (theDate.getMonth() + 1) + "-" + theDate.getDate() + " " +
			theDate.getHours() + ":" + theDate.getMinutes() + ":" + theDate.getSeconds();
	};
	
	/**
	 * YYYY-MM-DD HH:ii:SS
	 * @param dateString 字符串转时间
	 */
    public convertDateFromString(dateString:any){
		let DATE_REGEXP = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})([T\\s](\\d{2}):(\\d{2}):(\\d{2})(\\.(\\d{3}))?)?.*");
        if(DATE_REGEXP.test(dateString)){
            var timestamp = dateString.replace(DATE_REGEXP, function($all,$year,$month,$day,$part1,$hour,$minute,$second,$part2,$milliscond){
                var date = new Date($year, $month,$day, $hour||"00", $minute||"00", $second||"00", $milliscond||"00");
                return date.getTime();
            });
            var date = new Date();
            date.setTime(timestamp);
            return date;
        }
        return new Date();
	}
	
	/**
	 * 时区问题+8
	 * @param data 
	 */
	public dateToISOString(dateString:any){
		let convert = this.convertDateFromString(dateString);
		let goDate = new Date(+convert + 8 * 3600 * 1000);
		return goDate.toISOString();
	}

	//格式化时间
	public getCurrentDate(timestamp:any) {
		// 格式化日期，获取今天的日期
		let Dates = new Date(timestamp);
		let year = Dates.getFullYear();
		let month = (Dates.getMonth() + 1) < 10 ? '0' + (Dates.getMonth() + 1) : (Dates.getMonth() + 1);
		let day = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
		let hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
		let minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
		let seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
		return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
	};

	//格式化时间
	public getinitDate() {
		// 格式化日期，获取今天的日期
		let Dates = new Date(new Date().getTime());
		let year = Dates.getFullYear() + 1;
		let month = (Dates.getMonth() + 1) < 10 ? '0' + (Dates.getMonth() + 1) : (Dates.getMonth() + 1);
		let day = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();

		return year + '-' + month + '-' + day;
	};

	//获取页面跳转(这是非公众号才会使用)
	public isNotWechatToPage() {
		let ionic_data = this.urlParams("ionic");
		let that = this;
		setTimeout(() => {
			let nav = that.app.getActiveNav();
			let view = nav.getActive();
			if (ionic_data) {
				let json = that.base64Decrypt(ionic_data);
				let data = JSON.parse(json);//转为对象
				let page = data['page'];
				delete data['page'];
				//页面跳转
				nav.push(page, data);
				view._didLoad();
			}
		}, 1000);

	}

	//加密 
	public base64Encrypt(data: any) {
		return new Buffer(data).toString('base64');
	}

	//解密
	public base64Decrypt(data: any) {
		return new Buffer(data, 'base64').toString();
	}
}